package com.example.grpc.consume.client;

import com.example.grpc.generator.HelloRequest;
import com.example.grpc.generator.HelloResponse;
import com.example.grpc.generator.HelloServiceGrpc;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author liyuncong
 * @version 1.0
 * @file HelloClient
 * @brief HelloClient
 * @details HelloClient
 * @date 2022-11-08
 *
 * Edit History
 * ----------------------------------------------------------------------------
 * DATE                        NAME               DESCRIPTION
 * 2022-11-08                  liyuncong          Created
 */
public class HelloClient {

    private static final Logger logger = LoggerFactory.getLogger(HelloClient.class);

    public HelloResponse send(HelloRequest request) {
        String target = "localhost:50051";
        ManagedChannel channel = ManagedChannelBuilder
            .forTarget(target)
            .usePlaintext()
            .build();
        HelloServiceGrpc.HelloServiceBlockingStub blockingStub =
            HelloServiceGrpc.newBlockingStub(channel);

        return blockingStub.hello(request);
    }

    public static void main(String[] args) {
        HelloClient client = new HelloClient();
        final HelloRequest request = HelloRequest
            .newBuilder()
            .setName("name")
            .setGender("gender")
            .build();
        final HelloResponse send = client.send(request);
        logger.info("Response is " + send);
    }
}
